home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / graphics 2d / rotate bitmap 90 / rotate.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  4.6 KB  |  174 lines

  1. /*
  2.     File:        Rotate.c
  3.  
  4.     Contains:    Rotates a bitmap 90 degrees
  5.  
  6.     Written by:     
  7.  
  8.     Copyright:    Copyright © 1984-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/14/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23. #include <Dialogs.h>
  24. #include <Fonts.h>
  25. #include <Processes.h>
  26. #include <Sound.h>
  27.  
  28. #define    BIT00       0x01
  29. #define    BIT01       0x02
  30. #define    BIT02       0x04
  31. #define    BIT03       0x08
  32. #define    BIT04       0x10
  33. #define    BIT05       0x20
  34. #define    BIT06       0x40
  35. #define    BIT07       0x80
  36.  
  37. void rotate();
  38.  
  39. BitMap        *macBitMap;
  40.  
  41. void rotate()
  42. {
  43.     Ptr                srcPtr, destPtr, destColPtr, myBaseAddr;
  44.     BitMap            *newBitmap;
  45.     short            destRowBytes, bitchar, srcByte, srcH, srcV, srccols, srcrows, i;
  46.     Rect            destRect;
  47.  
  48.     /* rotate boundary rectangle */
  49.     srcH = macBitMap->bounds.right - macBitMap->bounds.left;
  50.     srcV = macBitMap->bounds.bottom - macBitMap->bounds.top;
  51.     SetRect(&destRect, macBitMap->bounds.left, macBitMap->bounds.top,
  52.             macBitMap->bounds.left+srcV, macBitMap->bounds.top+srcH);
  53.  
  54.     /* allocate destination buffer */
  55.     destRowBytes  = (srcV+7) >> 3;
  56.     newBitmap = (BitMap *) NewPtrClear(sizeof(BitMap));
  57.     myBaseAddr = (Ptr) NewPtrClear(destRowBytes*srcH);
  58.  
  59.     /* set-up src to rotated destination scan */
  60.     srccols = macBitMap->rowBytes;
  61.     srcrows = srcV;
  62.     srcPtr = macBitMap->baseAddr;
  63.     bitchar = BIT00;
  64.     destColPtr= myBaseAddr + destRowBytes - 1;
  65.     destPtr   = destColPtr;
  66.  
  67.     /* scan src row and rotate into dest col */
  68.     while (srcrows-- > 0) {
  69.         for (i=0; i<srccols; i++) {
  70.             srcByte = *(srcPtr++);
  71.             if (srcByte & BIT07) *destPtr |= bitchar;
  72.             destPtr += destRowBytes;
  73.             if (srcByte & BIT06) *destPtr |= bitchar;
  74.             destPtr += destRowBytes;
  75.             if (srcByte & BIT05) *destPtr |= bitchar;
  76.             destPtr += destRowBytes;
  77.             if (srcByte & BIT04) *destPtr |= bitchar;
  78.             destPtr += destRowBytes;
  79.             if (srcByte & BIT03) *destPtr |= bitchar;
  80.             destPtr += destRowBytes;
  81.             if (srcByte & BIT02) *destPtr |= bitchar;
  82.             destPtr += destRowBytes;
  83.             if (srcByte & BIT01) *destPtr |= bitchar;
  84.             destPtr += destRowBytes;
  85.             if (srcByte & BIT00) *destPtr |= bitchar;
  86.             destPtr += destRowBytes;
  87.         }
  88.         if (bitchar==BIT07) {
  89.             bitchar = BIT00;
  90.             destColPtr--;
  91.         } else {
  92.             bitchar <<= 1;
  93.         }
  94.  
  95.         destPtr = destColPtr;
  96.     }
  97.     
  98.     /* remove src bitmap (portbits for offscreen port) */
  99.     DisposePtr((Ptr)macBitMap->baseAddr);
  100.     DisposePtr((Ptr)macBitMap);
  101.     
  102.     /* store new src in object */
  103.     macBitMap   = (BitMap *)newBitmap;
  104.     macBitMap->baseAddr = (Ptr) myBaseAddr;
  105.     macBitMap->rowBytes = destRowBytes;
  106.     macBitMap->bounds   = destRect;
  107. }
  108.  
  109. void main()
  110. {
  111.     WindowPtr        mainWinPtr;
  112.     OSErr            error;
  113.     SysEnvRec        theWorld;
  114.     Rect            windRect, ovalRect, myBounds;
  115.     short            myRowBytes;
  116.     
  117.     /* Make sure ColorQD exists. */
  118.     error = SysEnvirons(1, &theWorld);
  119.     if (theWorld.hasColorQD == false) {
  120.         SysBeep(50);
  121.         ExitToShell();
  122.     }
  123.     
  124.     /* Initialize all the needed managers. */
  125.     InitGraf(&qd.thePort);
  126.     InitFonts();
  127.     InitWindows();
  128.     InitMenus();
  129.     TEInit();
  130.     InitDialogs(nil);
  131.     InitCursor();
  132.  
  133.     /* Define output window with an inset clip region. */
  134.     SetRect(&windRect, 100, 100, 404, 404);
  135.     mainWinPtr = NewWindow(nil, &windRect, "\pJohn", true, documentProc, (WindowPtr) -1, false, 0);
  136.     SetPort(mainWinPtr);
  137.  
  138.     SetRect(&windRect, 0, 0, 304, 304);
  139.     EraseRect(&windRect);
  140.     SetRect(&ovalRect, 15, 15, 60, 120);
  141.     PaintOval(&ovalRect);
  142.     MoveTo(10,140);
  143.     DrawString("\pHi there.");
  144.     MoveTo(0,0);
  145.     LineTo(303,303);
  146.     MoveTo(0,303);
  147.     LineTo(303,0);
  148.  
  149.     /* rotate boundary rectangle */
  150.     myBounds = windRect;
  151.     myRowBytes = (myBounds.right - myBounds.left + 7) >> 3;
  152.     macBitMap = (BitMap *) NewPtrClear(sizeof(BitMap));
  153.     macBitMap->baseAddr = (Ptr) NewPtrClear(myRowBytes * (myBounds.bottom - myBounds.top));
  154.     macBitMap->bounds = myBounds;
  155.     macBitMap->rowBytes = myRowBytes;
  156.  
  157.     CopyBits(&(*mainWinPtr).portBits, macBitMap, &windRect, &macBitMap->bounds, 0, nil);
  158.  
  159.     /* Wait until user clicks button. */
  160.     do {
  161.     } while (!Button());
  162.  
  163.     rotate();
  164.     CopyBits(macBitMap, &(*mainWinPtr).portBits, &macBitMap->bounds, &windRect, 0, nil);
  165.  
  166.     /* Wait for button to be up */
  167.     do {
  168.     } while (Button());
  169.     /* Wait until user clicks button. */
  170.     do {
  171.     } while (!Button());
  172. }
  173.  
  174.